package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Grants;
import com.karry.yl.service.baseinfo.GrantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 床位信息Controller
 */
@Controller
@RequestMapping("/grants")
public class GrantsController extends SuperController {
    @Autowired
    private GrantsService grantsService;
    /**
     * 到列表页
     * @return
     */
    @RequestMapping
    public String listUI(){
        return "baseinfo/grants/list";
    }

    @RequestMapping("/add")
    public String addUI(ModelMap map){
        return "baseinfo/grants/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Grants grants = grantsService.selectById(id);
        map.put("grants",grants);
        return "baseinfo/grants/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Grants> grantsPage = grantsService.selectPage(new Page<Grants>(page, limit),new EntityWrapper<Grants>().eq("orgId",currentUser.getOrgId()).orderBy("createdTime",false));
        return new LayTableResponse(grantsPage.getRecords(),grantsPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Grants grants){
        grants.setOrgId(currentUser.getOrgId());
        grants.setOrgName(currOrganization.getName());
        grantsService.insert(grants);
        return "redirect:/grants";
    }

    @RequestMapping("/update")
    public String update(Grants grants){
        grants.setOrgId(currentUser.getOrgId());
        grants.setOrgName(currOrganization.getName());
        grantsService.updateById(grants);
        return "redirect:/grants";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        grantsService.deleteBatchIds(ids);
        return "redirect:/grants";
    }
}
